package com.woniuxy.service.Impl;

import com.woniuxy.mapper.MoneyMapper;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.service.MoneyService;
import com.work.framework.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 * @title: MoneyServiceImpl
 * @projectName mashangpai
 * @description: TODO
 * @date 2019/9/1916:20
 */
@Service
public class MoneyServiceImpl implements MoneyService {

    @Autowired
    private MoneyMapper moneyMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Integer chargeMoney(Integer uid, BigDecimal money) {
        //查询账户余额
        Balance balance = moneyMapper.findBalance(uid);
        //金额比较
        BigDecimal ammount = balance.getAmount();

        if (money.compareTo(ammount) < 1) {
            //扣除金额
            money = ammount.subtract(money);
            //修改数据库
            Integer code = moneyMapper.updateMoney(balance.getId(), money);
            if (code == 1) {
                return 200;
            } else {
                return 500;
            }
        } else {
            return 501;
        }
    }

    @Override
    public Integer addMoney(Integer uid, BigDecimal money) {
        Balance balance = moneyMapper.findBalance(uid);
        BigDecimal amount = balance.getAmount();
        money = amount.add(money);
        Integer code = moneyMapper.updateMoney(balance.getId(), money);
        if (code == 1) {
            return 200;
        } else {
            return 500;
        }
    }


    @Override
    public Integer payOrder(HttpSession session, Integer oid, Integer sid) {
        //查找订单
        Order order = moneyMapper.findOrder(oid);
        if (order == null) {
            return 500;
        }
        User user = (User) session.getAttribute("user");

        if (!user.getUid().equals(order.getBid())) {
            return 500;
        }
        //消费总金额
        BigDecimal totaled = order.getTransactionPerice().add(order.getCharges());
        //押金
        Auction auction = moneyMapper.findAuctionById(order.getGid());
        BigDecimal cash = auction.getCash();
        //余额
        Balance balance = moneyMapper.findBalance(order.getBid());
        BigDecimal amount = balance.getAmount();
        if (amount.compareTo(totaled.subtract(cash)) > -1) {
            //扣除余额
            amount = amount.subtract(totaled.subtract(cash));
            //修改数据库余额
            moneyMapper.updateMoney(balance.getId(), amount);
            //修改订单状态
            orderMapper.updateOrder(oid, 4);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String payTime = simpleDateFormat.format(date);
            orderMapper.updatePayTime(payTime, sid, oid);
        } else {
            return 501;
        }
        return 200;
    }

}
